//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.northpool.operator.common.algorithm;


import cn.hutool.core.annotation.AnnotationUtil;
import com.northpool.operator.IOperatorHandler;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.param.Parameter;
import com.northpool.operator.utils.ParamUtils;
import com.northpool.operator.utils.ServeOperatorHelper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class Application extends AlgorithmBase {
    public Application() {
    }

    public static void main(String[] args) {
        try {
            System.out.println("插件运行参数如下: ");

            for (String arg : args) {
                System.out.println(arg);
            }

            if (ArrayUtils.isEmpty(args) || StringUtils.isBlank(args[0])) {
               throw new RuntimeException("算子类名称为空，参数错误");
            }
            Map<String, String> envMap = System.getenv();
            ParamTool.Builder paramToolBuilder = new ParamTool.Builder();
            Class<?> clz = Class.forName(args[0]);
            Object alg;
            if (ServeOperatorHelper.class.isAssignableFrom(clz)) {
                InputParams inputParams = AnnotationUtil.getAnnotation(clz.getConstructors()[0], InputParams.class);
                for (InputParam inputParam : inputParams.value()) {
                    String value = ParamUtils.parse(inputParam.ref());
                    paramToolBuilder.add(inputParam, value);
                }
                alg = clz.getConstructor(ParamTool.class).newInstance(paramToolBuilder.build());
            } else {
                alg = clz.getConstructor().newInstance();
            }

            if (alg instanceof Algorithm) {
                ((Algorithm)alg).execute(args);
            } else if(alg instanceof IOperatorHandler) {
                if (StringUtils.isNotEmpty(envMap.get("JM_INSTANCE_NAME"))) {
                    InputParams inputParams = AnnotationUtil.getAnnotation(alg.getClass().getConstructors()[0], InputParams.class);

                    for (InputParam inputParam : inputParams.value()) {
                        String value = ParamUtils.parse(inputParam.ref());
                        paramToolBuilder.add(inputParam, value);
                    }
                    ((IOperatorHandler)alg).execute(paramToolBuilder.build());
                } else {
                    Map<String, String> params = Helper.xmlArg2Map(args);
                    params.forEach((key, value) -> {
                        paramToolBuilder.add(key, Parameter.Type.STRING.newParameter(value, false));
                    });
                    ((IOperatorHandler)alg).execute(paramToolBuilder.build());
                }

            }
            // 持久化算子阻塞等待
            if (ServeOperatorHelper.class.isAssignableFrom(clz)) {
                Thread.sleep(Long.MAX_VALUE);
            } else {
                System.exit(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

    public String getHint() {
        return super.getHint();
    }

    protected void execute() throws Exception {
        this.parameter.forEach((k, v) -> {
            System.out.println(k + " ==== " + v);
        });
    }

    public String getName() {
        return "测试算法";
    }
}
